import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist';
// 本地化 可选localStore|sessionStorage 默认是localStore
const vuexLocal = new VuexPersistence({
  storage: window.localStorage
});
Vue.use(Vuex);
export default new Vuex.Store({
  plugins: [vuexLocal.plugin], //本地化
  state: {
    name: '',
    token: '',
    userInfo: {}
  },
  getters: {},
  actions: {
    async updateStoreData({ commit }, { name }) {
      commit('updateStore', { name });
    }
  },
  mutations: {
    updateStore(state, payload) {
      state = { ...payload };
    },
    setToken(state, token) {
      state.token = token;
    },
    setUserInfo(state, userInfo) {
      let roleType = null;
      if (userInfo.roleCode === 'ADMIN') {
        roleType = 1; //超级管理员
      } else if (userInfo.roleCode === 'ADMINISTRATOR') {
        roleType = 2; //普通管理员
      } else {
        roleType = 3; //普通员工
      }
      userInfo.roleType = roleType;
      state.userInfo = userInfo;
    }
  }
});
